index_app.controller("sales", function ($scope,$http) {
    $http.post("commodityType/selectAll").then(function(response){
		//数据源
		$scope.commodityType = response.data;
		$http.post("commodityDetailed/selectBySkuType?skuType="+$scope.commodityType[0].typeKey).then(function(response){
        	//数据源
        	$scope.commodityDetailed = response.data;

        });
	});
	   $http.post("stmUsr/selectAll").then(function(response){
        	//数据源
        	$scope.stmUsrList = response.data;
        });
    $('#myModal').modal('hide');
	$scope.commodityTypeClick = function(commodity){
        console.log(commodity);
        $http.post("commodityDetailed/selectBySkuType?skuType="+commodity.typeKey).then(function(response){
               //数据源
               $scope.commodityDetailed = response.data;

         });
	}
   // $scope.count = 0;
    $scope.key = 0;

	$scope.selectDetailed = function(commDetailed){
        if($("#countAll").is(":hidden")){
               $("#countAll").show();    //如果元素为隐藏,则将它显现
         }

	    if($("#comm_"+commDetailed.detailedKey).length > 0){//该元素存在
	        $scope.num = ($("#commPrice_"+commDetailed.detailedKey).text()*1)+1;
	        $("#commPrice_"+commDetailed.detailedKey).text($scope.num);
	    } else {
	        $scope.orderDiv="<div id='comm_"+commDetailed.detailedKey+"' style='float:left'><div   class='btn btn-app btn-info' style='width:210px'><span id='name_"+commDetailed.detailedKey+"'>"+commDetailed.name+"</span>"+
                         "<span class='detailedPrmKey' style='display:none'>"+commDetailed.detailedKey+"</span><span style='margin-left:10px'>￥<span id='amount_"+commDetailed.detailedKey+"'>"+commDetailed.price+"</span></span><span style='margin-left:10px'>数量:<span class='spanCount' id='commPrice_"+commDetailed.detailedKey+"'>1</span>件</span>"+
                        " <p class='countP' id='countTemp_"+commDetailed.detailedKey+"'></p><i onclick='deleteCount("+commDetailed.detailedKey+","+commDetailed.price+")' class='fa fa-minus-circle red'></i></div></div>";
	        //$(".detailedPrmKey").hide();
	        $("#order").append($scope.orderDiv);
	        $("#countTemp_"+commDetailed.detailedKey).hide();
	    }

        $scope.priceAll = (commDetailed.price)*1*($("#commPrice_"+commDetailed.detailedKey).text());
        $("#countTemp_"+commDetailed.detailedKey).text($scope.priceAll);

        //购物车总量
        var spanCount=$("span.spanCount");
        if(spanCount.length == 1){
            $scope.count = spanCount.eq(0).text();
            $scope.countTotal = $scope.count;
         } else {
              $scope.countTotal = 0;
              for (var i = 0;i< spanCount.length; i++) {
                      $scope.count = (spanCount.eq(i).text())*1;
                      //console.log(" $scope.veryLast "+ $scope.veryLast);
                      $scope.countTotal += $scope.count;
                 };
         }
        $("#countAll").text("共"+ $scope.countTotal+"件");
	}


	$scope.empty = function(){
	    $('#myModal').modal('hide');
	    $("#order").empty();
	    $("#countAll").hide();
	}
    //结算
    $scope.settlement = function(){
    $('#myModal').modal('show');
           //结算金额
                  var pCountP=$("p.countP");
                  if(pCountP.length == 1){
                       $scope.veryLast = pCountP.eq(0).text();
                       $scope.total = $scope.veryLast;
                  } else {
                      $scope.total = 0;
                       for (var i = 0;i< pCountP.length; i++) {
                            $scope.veryLast = (pCountP.eq(i).text())*1;
                            $scope.total += $scope.veryLast;
                       };
                  }
                  $scope.moneyTotal = $scope.total;
                  console.log("$scope.total "+$scope.total);
    }

    $scope.settlementAll = function(){
    //商品消费明细：订单号，会员类型，商品名，商品类型，单价，数量，总额，操作员，会员号，支付方式
        var spanDetailedPrmKey=$("span.detailedPrmKey");
             for (var i = 0;i< spanDetailedPrmKey.length; i++) {
                  $scope.prmKey = spanDetailedPrmKey.eq(i).text();//商品主键
                   console.log("$scope.prmKey "+$scope.prmKey);
                   $scope.commCount = $("#commPrice_"+$scope.prmKey).text();//数量
                   $scope.amount = $("#amount_"+$scope.prmKey).text();//商品单价
                   $scope.name = $("#name_"+$scope.prmKey).text();//商品名
                   $scope.sum = ($scope.amount*1) * ($scope.commCount*1);
                   console.log("$scope.sum "+$scope.sum);
             $scope.commDetailed = {
                    VipType:$scope.vipType,
                    SkuName:$scope.name,
                    detailedKey:$scope.prmKey,
                    price:$scope.amount,
                    quantity:$scope.commCount,
                    sum:$scope.sum,
                    usrKey:$scope.usrKey,
                    paymentMethod:$scope.payMethod,
                    vipNum:$scope.vipNum
                };
            $http({
					method  : 'POST',
					url     : 'commConsum/insertAll',
					data:$scope.commDetailed
				}).success(function(data) {
                    if(data=1){
                        $scope.title = '<h4 class="easyDialog_title" id="easyDialogTitle"><i class="icon-ok green"></i> <a href="javascript:void(0)" title="关闭窗口" class="close_btn" id="closeBtn">&times;</a>保存</h4>';
						//保存成功内容
						$scope.toolTip = "保存成功";
						toolTipDialog();
						 $("#order").empty();
                        $("#countAll").hide();
						$('#myModal').modal('hide');
					    //window.location.href="#sales";
                         $scope.vipPayTotal = "";
                         $scope.smallChange = "";
                         $scope.vipType = "";
                         $scope.payMethod = "";
                         $scope.usrKey = "";
                         $scope.vipNum = "";
                    } else if(data == -1){
                         $scope.title = "警告";
                         $scope.toolTip = "商品库存不足";
                         toolTipDialog();
                         $('#myModal').modal('hide');

                    } else {
                          $scope.title = "保存";
                          $scope.toolTip = "保存失败";
                           toolTipDialog();
                           $('#myModal').modal('hide');
                    }
				});

        };
        /*console.log("$scope.usrKey "+$scope.usrKey);//管理员ID
        console.log("$scope.payMethod "+$scope.payMethod);//支付方式
        console.log("$scope.vipType "+$scope.vipType);//会员类型*/

    }

    $scope.calculatedBalance = function(){
        if($scope.vipPayTotal*1 < $scope.moneyTotal*1){
            $scope.title = "警告";
                    $scope.toolTip = "请输入正确的会员付款金额";
                    toolTipDialog();
        } else {
            $scope.smallChange = $scope.vipPayTotal*1 - $scope.moneyTotal*1;
        }

    }
    function toolTipDialog(){
        		easyDialog.open({
        			container : {
        				header : $scope.title,
        				content : $scope.toolTip,
        				yesFn : $scope.toolFn,
        				noFn : false
        			},
        			fixed : false
        		});
        	}
        	//弹窗中确定按钮事件
        	$scope.toolFn = function(){
        		return true;
        	}
});
function deleteCount(detailedKey,price){
    var countTemp = ($("#commPrice_"+detailedKey).text())*1;
    countTemp -= 1;

    $("#commPrice_"+detailedKey).text(countTemp);
     var priceAll = (price)*1*($("#commPrice_"+detailedKey).text());
     $("#countTemp_"+detailedKey).text(priceAll);

    if(countTemp <= 0){
       $("#comm_"+detailedKey).remove();
    }
      //购物车总量
    countTotal = 0;
    var spanCount=$("span.spanCount");
    if(spanCount.length == 1){
        var count = spanCount.eq(0).text();
        countTotal = count;
     } else {

          for (var i = 0;i< spanCount.length; i++) {
                  count = (spanCount.eq(i).text())*1;
                  //console.log(" $scope.veryLast "+ $scope.veryLast);
                  countTotal += count;
             };
     }
    $("#countAll").text("共"+ countTotal+"件");
    //当购物车总量小于等于0时，隐藏掉总数
    if(countTotal <= 0){
         $("#countAll").hide();
    }

}

